import type { CurrentUser } from '@/types/loginType'
import { defineStore } from 'pinia'
import { ref, watchEffect } from 'vue'
export const useCurrActStroe = defineStore('useCurrActStroe', () => {
  const currUser = ref<CurrentUser>({
    id: '',
    name: '',
    img: '',
    gender: '',
    email: '',
    bio: '',
    prefer: ''
  })
  initCurrUser()

  // 初始化赋值
  function initCurrUser() {
    const value = localStorage.getItem('currUser')
    currUser.value = value !== null ? JSON.parse(value) : {}
  }
  // 存储
  function assignCurrUser(data: Partial<CurrentUser>) {
    for (const [key, value] of Object.entries(data)) {
      if (value === '' || value === undefined) {
        //
      } else {
        currUser.value[key as keyof CurrentUser] = value
      }
    }
    localStorage.setItem('currUser', JSON.stringify(currUser.value))
  }
  // 删除
  function removeCurrUser() {
    localStorage.removeItem('currUser')
  }

  return {
    currUser,
    assignCurrUser,
    removeCurrUser
  }
})
